package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.IReportService;
import com.sky.vo.TopTenVo;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;

@RestController
@RequestMapping("/admin/report")
@Slf4j
public class ReportController {
    @Autowired
    private IReportService iReportService;
    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */

    @GetMapping("/turnoverStatistics")
    public Result<TurnoverReportVO> turnoverStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end){
        TurnoverReportVO turnoverReportVO=iReportService.getTurnoverStatistics(begin,end);
        log.info("营业额数据统计:{},{}",begin,end);//测试成功
        return Result.success(turnoverReportVO);
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/userStatistics")
    public Result<UserReportVO> userStatistics(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ){
        UserReportVO userReportVO=iReportService.getUserStatistics(begin,end);
        log.info("营业额数据统计:{},{}",begin,end);//测试成功
        return Result.success(userReportVO);
    }

    /**
     * 销量排名topTen
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/top10")
    public Result<TopTenVo> topTen(
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            LocalDate end
    ){
        TopTenVo topTenVo=iReportService.topTen(begin,end);
        return Result.success(topTenVo);
    }

    /**
     * 导出运营数据报表
     * @param response
     * @return
     */
    @GetMapping("/export")
    public Result<String> export(HttpServletResponse response){
        iReportService.exportBusinessData(response);
        return Result.success("文件已经下载完毕!");
    }
}
